package research.ui.editors;

import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.events.IHyperlinkListener;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Hyperlink;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;

import research.entity.Entity;
import research.entity.EntityFactory;
import research.entity.EntityType;

public abstract class FormEntityEditor extends EntityEditor {
	
	protected FormToolkit toolkit;
	protected ScrolledForm form;

	@Override
	protected void render(Composite parent) {
		toolkit = new FormToolkit(parent.getDisplay());
		form = toolkit.createScrolledForm(parent);

		TableWrapLayout layout = new TableWrapLayout();
		layout.numColumns = 1;
		form.getBody().setLayout(layout);

		renderForm();
		
		List<EntityType> childs = entity.getType().getChildren();
		
		if (childs.size() > 0) {
			Section section = toolkit.createSection(form.getBody(), Section.TITLE_BAR
					| Section.TWISTIE | Section.EXPANDED);
			TableWrapData td = new TableWrapData(TableWrapData.FILL);
			section.setLayoutData(td);
			section.setText("Создать для " + entity.getType().getForName() + "...");
			Composite composite = toolkit.createComposite(section);
			section.setClient(composite);
			composite.setLayout(new GridLayout(1, false));
			
			for (EntityType type : childs) {
				createAddLink(type, composite);
			}
		}
	}
	
	@Override
	protected void setFormTitle(String title) {
		form.setText(title);
	}
	
	protected abstract void renderForm();
	
	protected void setComboEntity(ComboViewer combo, Entity entity) {
		entity = entity == null ? Entity.NULL_ENTITY : entity;
		combo.setSelection(new StructuredSelection(entity));
	}

	protected Entity getComboEntity(ComboViewer combo) {
		Entity entity = (Entity) ((StructuredSelection) combo.getSelection()).getFirstElement();
		return entity == Entity.NULL_ENTITY ? null : entity;
	}
	
	protected void createAddLink(EntityType type, Composite parent) {
		GridData gd = new GridData();
		Hyperlink link = toolkit.createHyperlink(parent, type.getName(), SWT.NONE);
		link.setLayoutData(gd);
		link.addHyperlinkListener(new AddHyperlinkListener(type));
	}
	
	public class AddHyperlinkListener implements IHyperlinkListener {
		private EntityType type;
		
		public AddHyperlinkListener(EntityType type) {
			super();
			this.type = type;
		}

		@Override
		public void linkActivated(HyperlinkEvent e) {
			EntityFactory.openNew(type, isNewEntity() ? null : entity);
		}

		@Override
		public void linkEntered(HyperlinkEvent e) {
		}

		@Override
		public void linkExited(HyperlinkEvent e) {
		}
	}
	
	protected static String parseDouble(Double d) {
		if (d == null)
			return "";
		return d.toString();
	}
	
	protected static Double parseString(String s) throws NumberFormatException {
		if (s == null || s.isEmpty())
			return null;
		return Double.parseDouble(s);
	}
	
	protected boolean validateText(Text text) {
		boolean flag = text.getText() != null && !text.getText().isEmpty();
		if (!flag) {
			MessageDialog.openWarning(getSite().getShell(), "Ошибка ввода",
				"Необходимо заполнить обязательные поля");
		}
		return flag;
	}
}
